package com.yy.QSManage.api;

import com.yy.QSManage.common.Result;
import com.yy.QSManage.exception.ServiceException;
import com.yy.QSManage.model.dto.FieldDto;
import com.yy.QSManage.model.dto.OptionsDto;
import com.yy.QSManage.model.entity.FaSysDataSource;
import com.yy.QSManage.model.entity.FaSysOption;
import com.yy.QSManage.service.FaSysDataSourceService;
import com.yy.QSManage.service.FaSysOptionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@RestController
@RequestMapping("/FaSysOptionApi")
@Api(tags = "选项")
public class FaSysOptionApi extends BaseApi<FaSysOption>{

    // 判断是否包含非法关键字，防止sql注入
    final String[] keywords = {" from ", " where ", " delete "};

    @Autowired
    FaSysOptionService optionService;
    @Autowired
    FaSysDataSourceService dataSourceService;

    @Override
    public Result<Boolean> add(@RequestBody FaSysOption query) {
        // 判断是否重复
        optionService.ifExist(query);
        // 非法字符检查,防止sql注入
        illegalCharactersCheck(query);
        // 树状格式检查
        treeChech(query);
        return super.add(query);
    }

    @Override
    public Result<Boolean> edit(@RequestBody FaSysOption query) {
        // 非法字符检查,防止sql注入
        illegalCharactersCheck(query);
        // 树状格式检查
        treeChech(query);
        return super.edit(query);
    }

    @ApiOperation("获取选项数据源")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getOptions/{id}")
    public Result<List<OptionsDto>> getOptions(@PathVariable String id) {
        return Result.success(optionService.getOptions(id));
    }

    /**
     * 非法字符检查,防止sql注入
     * @param option
     */
    public void illegalCharactersCheck(FaSysOption option){
        List<String> keywordList = Arrays.asList(keywords);
        String keyField = option.getKeyField();
        for (String keyword : keywordList) {
            if(keyField.contains(keyword)){
                throw new ServiceException("包含非法字符");
            }
        }
    }

    /**
     * 树状格式检查
     * @param option
     */
    public void treeChech(FaSysOption option){
        if(option.getTree().equals(1)){
            String dataSourceId = option.getDataSourceId();
            FaSysDataSource dataSource = dataSourceService.getById(dataSourceId);
            if(!"enum".equals(dataSource.getType())){
                String source = dataSource.getSource();
                List<FieldDto> fields = dataSourceService.fields(source);
                AtomicBoolean ifTree = new AtomicBoolean(false);
                fields.forEach(fieldDto -> {
                    if("pid".equals(fieldDto.getColumn())){
                        ifTree.set(true);
                    }
                });
                if(!ifTree.get()){
                    throw new ServiceException("该数据源非树状结构！");
                }
            }
        }
    }
}
